//
// Created by Jisam on 2024/7/26 23:06.
// solve b
#include <bits/stdc++.h>

#define PSI pair<string,int>
#define PII pair<int,int>
#define PDI pair<double,int>
#define PDD pair<double,double>
#define VVI vector<vector<int>>
#define VI vector<int>
#define VS vector<string>

#define PQLI priority_queue<int, vector<int>, less<int>>
#define PQGI priority_queue<int, vector<int>, greater<int>>
#define i64 long long

using namespace std;

const int N = 1e5 + 5;
i64 ans;

void solve() {
    int n, k;
    cin >> n >> k;
    string s;
    vector<string> a(n, "");
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
//    for(int i = 1; i <= n ; i ++){
//        for(int j = 1 ; j <= n ; j ++){
//            cout << a[i][j] << " ";
//        }
//    }

    for (int i = 0; i < n; i += k) {
        for (int j = 0; j < n; j += k) {
            cout << a[i][j];
        }
        cout << "\n";
    }
//    cout<< "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    cin >> T;
    while (T--) solve();
//    cout << "\n"[];
    return 0;
}